#封装接口，让test_shopping页面更整洁
from common.request_util import send
from common.file_util import *

def search_goods(name,goods_keyword,page=1,limit=8):
    data = {
        'path': '/api/products',
        'method': 'get',
        'params': {'keyword': goods_keyword, 'page': page, 'limit': limit}
    }
    return send(name,**data)

#添加用户分组封装
def add_group(name,group_name):
    data = {
        'path': '/adminapi/user/user_group/save.html',
        'method': 'POST',
        'headers': {
            'Authori-zation': f'Bearer {read_extract_variable("manage_token")}'
        },
        'json': {"group_name": group_name}
    }
    return send(name,**data)

def add_coupon(name,coupon_name,price,permanent,permanent_count,status):
    data = {
        'path': '/adminapi/marketing/coupon/save_coupon',
        'method': 'POST',
        'json': {"coupon_title": coupon_name, "coupon_price": price, "type": 0, "use_min_price": 0,
                 "coupon_time": 7, "start_use_time": 0, "end_use_time": 0, "start_time": 0, "end_time": 0,
                 "receive_type": 3, "is_permanent": permanent, "total_count": permanent_count,
                 "sort": 0, "status": status, "product_id": "", "category_id": 0},
        'headers': {
            'Authori-zation': f'Bearer {read_extract_variable("manage_token")}'}
        }
    return send(name,**data)

#添加购物车
def add_cart(name,testdata):
    data = {
        'path':'/api/cart/add',
        'method':'post',
        'headers': {"Authori-zation":f'Bearer {read_extract_variable("token")}'
    },
        'json':{"productId":"11","cartNum":1,"new":0,"uniqueId":"76eedee7","virtual_type":0}
    }
    return send(name,**data)

#收藏商品
def collect_goods(name,id):
    data = {
        'path': '/api/collect/add',
        'method': 'post',
        'headers': {"Authori-zation":f'Bearer {read_extract_variable("token")}'
    },
        'json': {"id":id,"product":"product"}
    }
    return send(id,**data)

def confirm(name,cartId):
    data = {
        'path': '/api/order/confirm',
        'method': 'post',
        'headers': {'Authori-zation':f'Bearer {read_extract_variable("token")}'},
        'json': {"cartId":f"{cartId}","new":0,"addressId":0}
    }
    return send(name,**data)

#用户端结算
def settlement(name,orderKey):
    data = {
        'path':'/api/order/create/'f'{orderKey}',
        'method':'post',
        'headers': {"Authori-zation":f'Bearer {read_extract_variable("token")}'},
        'json':{"real_name":"刘子怡","phone":"13654226000","addressId":3,"formId":"","couponId":0,"payType":"offline","useIntegral":False,"bargainId":0,"combinationId":0,"discountId":None,"pinkId":0,"advanceId":0,"seckill_id":0,"mark":"","from":"weixinh5","shipping_type":1,"new":0,"invoice_id":"","quitUrl":"http://47.110.161.180/pages/order_pay_status/index?&type=3&totalPrice=19"}
    }
    return send(name,**data)

#管理端查询订单
def manage_order_list(name,orderId):
    data = {
        'path':f'/adminapi/order/list?page=1&limit=10&status=&pay_type=&data=&real_name={orderId}&field_key=all&type=',
        'method':'get',
        'headers': {"Authori-zation":f'Bearer {read_extract_variable("manage_token")}'}
    }
    return send(name,**data)

#管理端确认线下付款
def manage_confirm_payment(name,id):
    data ={
        'path': f'/adminapi/order/pay_offline/{id}',
        'method': 'post',
        'headers': {"Authori-zation": f'Bearer {read_extract_variable("manage_token")}'}
    }
    return send(name, **data)

#管理端发货
def manage_delivery(name,id):
    data = {
        'path': f'/adminapi/order/delivery/{id}',
        'method': 'put',
        'headers': {"Authori-zation": f'Bearer {read_extract_variable("manage_token")}'},
        'json':{"type":"1","express_record_type":"1","delivery_name":"宅急送","delivery_id":"123456789","express_temp_id":"","to_name":"","to_tel":"","to_addr":"","sh_delivery":"","fictitious_content":"","id":"","to_add":"","export_open":True,"delivery_code":"zhaijisong"}
    }
    return send(name, **data)

#用户端查看订单详情
def user_order_detail(name,orderId):
    data = {
        'path': f'/api/order/detail/{orderId}',
        'method':'get',
        'headers': {"Authori-zation": f'Bearer {read_extract_variable("token")}'},
    }
    return send(name, **data)

#用户端确认收货
def user_confirm_receipt(name,orderId):
    data = {
        'path':'/api/order/take',
        'method':'post',
        'headers': {"Authori-zation": f'Bearer {read_extract_variable("token")}'},
        'json':{"uni":f"{orderId}"}
    }
    return send(name, **data)


#用户端评价
def user_comment(name,unique):
    data = {
        'path':'/api/order/comment',
        'method':'post',
        'headers': {"Authori-zation": f'Bearer {read_extract_variable("token")}'},
        'json':{"comment":"111","product_score":5,"service_score":5,"pics":[],"unique":f"{unique}"}
    }
    return send(name, **data)

#用户端删除订单
def user_delete_order(name,orderId):
    data = {
        'path':'/api/order/del',
        'method':'post',
        'headers': {"Authori-zation": f'Bearer {read_extract_variable("token")}'},
        'json':{"uni":f"{orderId}"}
    }
    return send(name, **data)

#管理端删除订单
def manage_delete_order(name,id):
    data = {
        'path':f'/adminapi/order/del/{id}',
        'method': 'delete',
        'headers': {"Authori-zation": f'Bearer {read_extract_variable("manage_token")}'}
    }
    return send(name, **data)